package com.saku.uiprojecttest.activities;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.saku.uiprojecttest.R;
import com.saku.uiprojecttest.bean.Test;
import com.saku.uiprojecttest.views.TagLayout;

import java.util.List;
import java.util.Random;

/**
 * User: Missa
 * Date: 2016-12-20
 * Time: 16:59
 * Description:
 */
public class TestRecyclerAdapter extends RecyclerView.Adapter<TestRecyclerAdapter.TestViewHolder> {
    private List<Test> data;
    private Context mCtx;
    private Random r = new Random(255);

    public TestRecyclerAdapter(Context ctx, List<Test> data) {
        this.mCtx = ctx;
        this.data = data;
    }

    @Override
    public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mCtx).inflate(R.layout.test_item_layout, parent, false);
        TestViewHolder vh = new TestViewHolder(view);

        return vh;
    }

    @Override
    public void onBindViewHolder(TestViewHolder holder, int position) {
        holder.tagLayout.removeAllViews();
        for (int i = 0; i < data.get(position).tags.length; i++) {
            Log.d("lm", "onBindViewHolder: i = " + i);
            TextView tv = new TextView(mCtx.getApplicationContext());
            tv.setText(data.get(position).tags[i]);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            tv.setGravity(Gravity.CENTER);
            tv.setPadding(10, 10, 10, 10);
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.RECTANGLE);
            drawable.mutate();
            drawable.setCornerRadius(10);
            drawable.setColor(Color.rgb(r.nextInt(), r.nextInt(), r.nextInt()));
            tv.setBackground(drawable);
            holder.tagLayout.addView(tv);
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class TestViewHolder extends RecyclerView.ViewHolder {

        public TagLayout tagLayout;

        public TestViewHolder(View itemView) {
            super(itemView);

            tagLayout = (TagLayout) itemView.findViewById(R.id.rtc_tl);
        }
    }
}
